$(function() {
    var text;
    text = $('.header-title').text();
    homeLoad();
    $('.container-left li').click(function() {
        text = $(this).text();
        homeLoad();

    })

    function homeLoad() {
        if (text == "系统首页") {
            var title = [];
            $.ajaxSetup({
                headers: {
                    "Authorization": sessionStorage.getItem('token')
                }
            });
            //发送get请求获取数据
            $.ajax({
                url: 'http://47.96.133.125:7788/article/cascadeFindAll',
                type: 'GET',
                success: function(res) {
                    // console.log(res);
                    if (res.status == 200) {
                        //接收数据
                        var data = res.data;
                        console.log(data);
                        //遍历数组，筛选数组转为obj对象
                        data.forEach(function(item, index) {

                            var obj = {
                                title: data[index].title,
                                name: item.category.name,
                                content: item.content,
                                id: item.id
                            }
                            title.push(obj);

                        })
                        console.log(title);

                        var result1 = title.filter(function(item, index, arr) {
                            return item.name == "校园新闻"
                        });
                        var result2 = title.filter(function(item, index, arr) {
                            return item.name == "通知公告"
                        });
                        var result3 = title.filter(function(item, index, arr) {
                            return item.name == "学术活动"
                        });
                        // console.log("111", result3);
                        if (result1.length > 0) {
                            //当数据不为零时，清空框体
                            $("#homeListOne .home-null").remove();
                            $('.homeTitle1').text((result1.length));
                            //遍历返回数据，将数据追加到框体中
                            result1.forEach(function(item, index) {
                                // console.log(item.title);
                                var newTr = $(`<li data-toggle="modal" data-target="#homeModal" value="${result1[index].id}">
                        <a href="#">${result1[index].title}</a>
                        </li>`);
                                $('#homeListOne').append(newTr);
                            })
                        }
                        if (result2.length > 0) {
                            $("#homeListTwo .home-null").remove();
                            $('.homeTitle2').text((result2.length));
                            result2.forEach(function(item, index) {
                                // console.log(item.title);
                                var newTr = $(`<li data-toggle="modal" data-target="#homeModal" value="${result2[index].id}">
                        <a href="#" >${result2[index].title}</a>
                        </li>`);
                                $('#homeListTwo').append(newTr);
                            })
                        }
                        if (result3.length > 0) {
                            $("#homeListThree .home-null").remove();
                            $('.homeTitle3').text((result3.length));
                            result3.forEach(function(item, index) {
                                // console.log(item.title);
                                var newTr = $(`<li data-toggle="modal" data-target="#homeModal" value="${result3[index].id}">
                        <a href="#">${result3[index].title}</a>
                        </li>`);
                                $('#homeListThree').append(newTr);
                            })
                        }

                    }

                    $('.home-list li').click(function() {
                        $('#homeModal .modal-body').empty();
                        $('#homeModal .modal-title').empty();
                        var id = $(this).val();
                        console.log(id);
                        var result = data.filter(function(item, index, arr) {
                            return item.id == id;
                        });
                        var newTr = result[0].content;
                        console.log(newTr);
                        $('#homeModal .modal-title').append(result[0].title);
                        $('#homeModal .modal-body').append(newTr);
                    })
                }
            })

        }
    }
})